package com.wzcl.app.service.impl;


import com.alibaba.fastjson.JSON;
import com.wzcl.app.dao.model.AppUser;
import com.wzcl.app.model.common.BusinessException;
import com.wzcl.app.model.common.CodeMsg;
import com.wzcl.app.model.common.ValidateCode;
import com.wzcl.app.model.dto.UserDto;
import com.wzcl.app.service.RegisterService;
import com.wzcl.app.service.UserService;
import com.wzcl.app.service.common.AliMsgService;
import com.wzcl.app.utils.RedisUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Log4j2
@Service
public class RegisterServiceImpl implements RegisterService {

    public final static String VALIDATE_CODE_KEY = "_VALIDATE_CODE";
    public final static String VALIDATE_CODE_LOCK = "_VALIDATE_CODE_LOCK";

    @Autowired
    private UserService userService;

    @Autowired
    private AliMsgService aliMsgService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public AppUser forgetPwd(UserDto userDto) {
        //校验
        if (StringUtils.isBlank(userDto.getPhone())) {
            throw new BusinessException(CodeMsg.MUST_BE_NOT_NULL, "缺少手机号");
        }
        if (StringUtils.isBlank(userDto.getCode())) {
            throw new BusinessException(CodeMsg.MUST_BE_NOT_NULL, "缺少验证码");
        }
        if (StringUtils.isBlank(userDto.getPassword())) {
            throw new BusinessException(CodeMsg.MUST_BE_NOT_NULL, "缺少密码");
        }
        /*获取验证码，进行比对*/
        Object object = redisUtil.get(userDto.getPhone() + VALIDATE_CODE_KEY);
        if (object == null) {
            throw new BusinessException(CodeMsg.CODE_NOT_SEND);
        }
        ValidateCode validateCode = JSON.parseObject(String.valueOf(object), ValidateCode.class);
        if (!StringUtils.equals(userDto.getCode(), String.valueOf(validateCode.getCode()))) {
            throw new BusinessException(CodeMsg.CODE_NOT_MATCH);
        }
        AppUser user = userService.editPwd(userDto.getPhone(), userDto.getPassword());
        //删除缓存的验证码
        if (redisUtil.hasKey(userDto.getPhone() + VALIDATE_CODE_LOCK)) {
            redisUtil.del(userDto.getPhone() + VALIDATE_CODE_LOCK);
        }
        redisUtil.del(userDto.getPhone() + VALIDATE_CODE_KEY);
        return user;
    }

    @Override
    public AppUser registerUser(UserDto userDto) {
        //校验
        if (StringUtils.isBlank(userDto.getPhone())) {
            throw new BusinessException(CodeMsg.MUST_BE_NOT_NULL, "缺少手机号");
        }
        if (StringUtils.isBlank(userDto.getCode())) {
            throw new BusinessException(CodeMsg.MUST_BE_NOT_NULL, "缺少验证码");
        }
        if (StringUtils.isBlank(userDto.getPassword())) {
            throw new BusinessException(CodeMsg.MUST_BE_NOT_NULL, "缺少密码");
        }
        /*获取验证码，进行比对*/
        Object object = redisUtil.get(userDto.getPhone() + VALIDATE_CODE_KEY);
        if (object == null) {
            throw new BusinessException(CodeMsg.CODE_NOT_SEND);
        }
        ValidateCode validateCode = JSON.parseObject(String.valueOf(object), ValidateCode.class);
        if (!StringUtils.equals(userDto.getCode(), String.valueOf(validateCode.getCode()))) {
            throw new BusinessException(CodeMsg.CODE_NOT_MATCH);
        }
        AppUser user = userService.registerUser(userDto);
        //删除缓存的验证码
        if (redisUtil.hasKey(userDto.getPhone() + VALIDATE_CODE_LOCK)) {
            redisUtil.del(userDto.getPhone() + VALIDATE_CODE_LOCK);
        }
        redisUtil.del(userDto.getPhone() + VALIDATE_CODE_KEY);
        return user;
    }

    /**
     * 发送短信
     *
     * @param phone 用户手机号
     * @return Code
     * @throws Exception 短信发送异常
     */
    @Override
    public String sendMsg(String phone) {
        /*先去Redis中查询该手机号的验证码是否已经发送*/
        Object s = redisUtil.get(phone + VALIDATE_CODE_LOCK);
        if (s != null) {
            return null;
        }
        /*生成6位数字验证码*/
        ValidateCode generate = ValidateCode.generate(5);
        /*发送短信*/
        Boolean ok = aliMsgService.sendMsg(phone, generate.getCode());
        /*将验证码放入Redis，设置5分钟过期时间*/
        if (ok) {
            redisUtil.set(phone + VALIDATE_CODE_LOCK, phone, 60);
            redisUtil.set(phone + VALIDATE_CODE_KEY, JSON.toJSONString(generate), 5 * 60);
        } else {
            throw new BusinessException(CodeMsg.MSG_SEND_FAIL);
        }
        return generate.getCode();
    }
}
